isFavProOpened = false;

$(document).ready(function(){
    isBind = false;
	initBrachesTab();
	initTab();
	initMenu();
	initNumberCol();
	initTableRowAlter();
	initCarProfileMenu();
	initJqueryPlaceholder();
	initDatePicker();
	//initChatIcon();
	initArrowUp();
	initFloatingBanner();
	updateScrollBar();
	initFavouriteList();
	initEmailSubscriptionLayout();
	
	$(".styled-scroll-wrapper").mCustomScrollbar({
		autoDraggerLength:false,
        advanced:{
            updateOnContentResize: true
        }
	});
    $("div[id*='tabs']").mCustomScrollbar({
		autoDraggerLength:false,
        set_height:"120px"
	});
	setTimeout(function() {
		$('div#msg').fadeOut(3000);
    }, 3000);
	
	
});

function initBrachesTab(){
	$('#branches').tabs({
        activate: function( event, ui ) {
            /*
            $("div[id*='tabs']").mCustomScrollbar({
        		autoDraggerLength:false,
                set_height:"120px"
        	});
            */
            console.log(ui);
            ui.newPanel.mCustomScrollbar("update");
            /*
            ui.newPanel.mCustomScrollbar({
        		autoDraggerLength:false,
                set_height:"120px"
        	});
            */
        }
    }).scrollabletab({
		'animationSpeed':50, //Default 100
		'LastTab':false, //Default false
		'resizable':true, //Default false
		'resizeHandles':'e,s,se', //Default 'e,s,se'
		'easing':'easeInOutExpo'
	});
}

function initTab(){
	$('#tabs').tabify();
}

function initJqueryPlaceholder(){
	$('input[placeholder]').simplePlaceholder();
	$('textarea[placeholder]').simplePlaceholder();
}

function initMenu(){
	$('.menu').find('ul').hide();	
	$(".menu li a").each(function() {
		if ($(this).next().length > 0) $(this).addClass("parent");
	});
	
	$("a.active + ul").show();
	
	$(".toggleMenu").click(function(e) {
		$(this).toggleClass("active");
		$(".menu").toggle();
	});
	
	if ($(".toggleMenu").hasClass("active")) $(".menu").show();
	else $(".menu").hide();
	
	$(".menu li a.parent").unbind("click").bind("click", function(e) {
		//$(this).children("ul").stop(true, true).slideDown("slow");
		//$(this).children("a").addClass("parent");
		//e.preventDefault();
		var subMenu = $(this).siblings("ul");
		
		if($(subMenu).is(":visible")){
			$(subMenu).hide();
			$(subMenu).find("ul").hide();
			$(subMenu).find("a").removeClass("active");
		} else $(subMenu).show();
		
		if($(this).hasClass("active")) $(this).removeClass("active");
		else $(this).addClass("active");
	});
	
	//$(".menu li").bind("mouseleave", function(e) {
		//$(this).children("ul").stop(true, true).slideUp("slow");
		//$(this).children("a").removeClass("parent");
		//$(subMenu).hide();
		//$(subMenu).find("ul").hide();
		//$(subMenu).find("a").removeClass("active");
	//});
	
	//$(".menu").bind('mouseleave', function(e) { 
		//$(this).find("ul").hide(); 
		//$(this).find("a").removeClass("active");
	//});
}

function initCarProfileMenu(){
	$(".hello-block").hover(
		function() {
			$(".cart-profile-menu-wrapper").addClass("active");
			$(".hello-block").css("display", "none");
			
			$(".cart-profile-menu-wrapper").hover(function(){},
				function(){ $(this).removeClass("active");
				$(".hello-block").css("display", "block");
			});
	});
}

function initTableRowAlter() {
	$("#cart table tbody tr:odd").addClass("odd");
	$("#cart table tbody tr:even").addClass("even");
}

function initNumberCol() {
	count = 1;
	$(".no").each(function() {
		$(this).html(count++);
	});
}

function submitForm(formId, url, method) {
	$("#" + formId).attr("method", method);
	$("#" + formId).attr("action", url);
	$("#" + formId).submit();
}

function submitAction(url){
	window.location.href = url;
}

function initDatePicker(){
	$(".datepicker").datepicker();
}

function initChatIcon(){
    /*
	var isOpened = false;
	$(".chat-icon").click(function(){
		if(isOpened === false){
			$(".live-support-wrapper").animate({right: '10px'});
			$(this).css("opacity", 0.5);
			isOpened = true;
		} else {
			$(".live-support-wrapper").animate({right: '-200px'});
			$(this).css("opacity", 1);
			isOpened = false;
		}
	});
    */
    setTimeout(function(){
        $(".live-support-wrapper").animate({'bottom': '0px'}, 'slow');
    },2000);
}

function initArrowUp(){
	$(".arrow-up-icon").click(function(){
		$("html, body").animate({ scrollTop: 0 }, 800);
    	return false;
	});
}



function likeProduct(divFavBtn,proid){
    if(userid != "0"){
        $.ajax({
            type: "POST",
            url: "./action/ajax/likeProductAction.php",
            data: { proid:proid, user:userid },
            dataType: "json",
            async:false
        }).done(function( data ) {
            if(data.success == "1"){
                //alert("Cám ơn bạn đã yêu thích sản phẩm");
                $(divFavBtn).addClass("active");
                $(divFavBtn).removeAttr("onclick");
				var numOfLike = $(".favourite-icon").html();
				$(".favourite-icon").html(++numOfLike);
                
                $.ajax({
            		type: "GET",
            		url: "./action/ajax/getFavouriteProductProcess.php",
                    async:false
            	}).done(function(data){
            	   cur_content = $(".favourite-list-wrapper").find(".mCSB_container").html();
            	   if(cur_content != null){
                       $(".favourite-list-wrapper").show();
                       $(".favourite-list-wrapper").find(".mCSB_container").html(data);
                       $(".favourite-list-wrapper").mCustomScrollbar("update");
                       $(".favourite-list-wrapper").hide();
            	   }else{
                       $(".favourite-list-wrapper").show();
            	       $(".favourite-list-wrapper").html(data);
                       $(".favourite-list-wrapper").mCustomScrollbar();
                       $(".favourite-list-wrapper").hide();
            	   }
                   
                   
            	});
                
            }
            else{
                alert("Đánh giá không thành công, vui lòng thử lại sau");
            }
        });
        
    }
}

function flyToCart(id){
    fly($('.cart-wrapper'), $('.image-source img#' + id), 226, 226);
}

function flyToFavourite(id){
	$("#btn-" + id).html("<img src='image/layout/favourite-icon-hover.png' id='fav-"+ id +"'/>");
	var flyingOjbect = $("#fav-" + id);
	fly($(".favourite-wrapper"), flyingOjbect, 27, 27);
	flyingOjbect.remove();
}

function fly(container, flyingObject, w, h){    
	if (flyingObject) {
		var clone = flyingObject.clone()
			.offset({
			top: flyingObject.offset().top,
			left: flyingObject.offset().left
		}).css({'opacity': '0.9',
				'position': 'absolute',
				'height': h + 'px',
				'width': w +'px',
				'z-index': '100',
                'margin': 0 + 'px' // fix bug: in product detail, clonned object cannot get real top and left position 
		}).appendTo($('body'))
		  .animate({'top': container.offset().top + 10,
					'left': container.offset().left + 10,
					'width': w,
					'height': h
		}, 2500, 'easeInOutExpo');
		
		setTimeout(function () {
			container.effect("shake", {
				times: 1
			}, 200);
		}, 1500);

		clone.animate({
			'width': 0, 'height': 0
		}, function () {
			$(this).detach();
		});
	}
}

function updateScrollBar(){
    loadFavouriteProduct();
    $(".favourite-wrapper").on("click",function(){
        var list = $(".favourite-list-wrapper"); 
        if(!isBind){
            list.mCustomScrollbar({autoDraggerLength: false});
            list.find(".mCSB_container").css("margin-right", "5px");
            list.find(".mCSB_dragger").css("right", "3px");
            list.find(".mCSB_dragger_bar").css("width", "8px");
			list.find(".mCSB_draggerRail").css("background", "#ccc");
        }
        isBind = true;       
    });
    
    
    
    
	//$('#myModal').on('shown.bs.modal', function () {
	   //loadFavouriteProduct();
       
       //$(".styled-scroll-wrapper").mCustomScrollbar({
       //     autoDraggerLength:false
	   //});
       //$(".styled-scroll-wrapper").mCustomScrollbar("update");
    //});
}

function loadFavouriteProduct(){
	$.ajax({
		type: "GET",
		url: "./action/ajax/getFavouriteProductProcess.php",
        async:false
	}).done(function(data){
		//$("#favorite_modal_body").html(data);
        $(".favourite-list-wrapper").html(data);
	});
}

function initFloatingBanner(){
	$(".left-floating-banner").addFloating({  
		targetTop: 0,  
		snap: false  
	});
		
	$(".right-floating-banner").addFloating({  
		targetTop: 0,  
		snap: false  
    });
}

function initFavouriteList(){
	$(".favourite-icon").click(function(){
		var list = $(".favourite-list-wrapper");
		if(isFavProOpened){
			list.fadeOut(500);
			isFavProOpened = false;
		} else {
			list.fadeIn(500);
			isFavProOpened = true;
		}
	});
}

function initEmailSubscriptionLayout(){
	$("#email-subscription").focus(function(){
		$(".security-code").fadeIn(500);
	});
}